package com.shadt.pay.notity;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shadt.core.controller.BaseController;
import com.shadt.core.util.HttpsServiceRequest;
import com.shadt.core.util.IServiceRequest;
import com.shadt.core.util.XMLUtil;
import com.shadt.pay.wechat.entity.Pay_WeChat_Notity;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;


@Controller
@RequestMapping("/devshadt")
public class TestNotifyController extends BaseController{
	
	public static void main(String[] args) {
	
	}
	
	@RequestMapping(value="/notify")
	public void Notify(HttpServletRequest request,HttpServletResponse response,ServletInputStream inputStream) throws Exception{
		String respXml  = XMLUtil.inputStreamToString(inputStream);
		XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));// 解决XStream对出现双下划线的bug
		xs.alias("xml", Pay_WeChat_Notity.class);
		Pay_WeChat_Notity wReturn = (Pay_WeChat_Notity) xs.fromXML(respXml);
		IServiceRequest is = new HttpsServiceRequest();
		String prePayReturn =is.sendPost("http://192.168.0.216:8020/shadtpay/wechat/notity", wReturn);
		System.out.println(prePayReturn);
		if(wReturn.getReturn_code().equals("SUCCESS")){
			response.getWriter().print("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
		}else{
			response.getWriter().print("<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[参数格式校验错误]]></return_msg></xml>");
		}
	}
	
}
